﻿using System;
using System.Web;
using System.Web.Mvc;
using Hasiu.Entity;
using Hasiu.Service;
using Hasiu.Mvc.Filters;
using Hasiu.Mvc.ViewModels;
using Hasiu.Mvc.Infrastructure;
using Hasiu.Mvc.Extensions.Pager;

namespace Hasiu.Mvc.Controllers.Admin
{
    [AdminAuthorize]
    public class ProductAdminController : Controller
    {
        public ActionResult Type(string id)
        {
            var productTypeViewModel = new ProductTypeViewModel
                                           {
                                               GetProductTypeList = ProductTypeService._.GetList()
                                           };
            if (!string.IsNullOrEmpty(id))
            {
                productTypeViewModel.GetProductType = ProductTypeService._.Get(id);
            }
            return View(productTypeViewModel);
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Type(ProductType productType)
        {
            if (string.IsNullOrEmpty(productType.Id))
            {
                productType.Id = Guid.NewGuid().ToString("N");
                ProductTypeService._.Save(productType);
                return new RedirectOfAddResult("/admin/product/type.aspx");
            }
            ProductTypeService._.Update(productType);
            return new RedirectOfUpdateResult("/admin/product/type.aspx");
        }

        public ActionResult DelType(string id)
        {
            ProductTypeService._.Delete(id);
            return new RedirectOfDeleteResult("/admin/product/type.aspx");
        }

        public ActionResult Save(string id)
        {
            Product product = null;
            if (!string.IsNullOrEmpty(id))
            {
                product = ProductService._.Get(id);
            }
            return View(product);
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Save(Product product, string returnUrl)
        {
            if (string.IsNullOrEmpty(product.Id))
            {
                product.Id = Guid.NewGuid().ToString("N");
                product.AddTime = DateTime.Now;
                product.Py = product.Name.HzToPySzm() + " " + product.Name.HzToPy();
                ProductService._.Save(product);
                return new RedirectOfOtherResult("添加成功，请上传苗木图片！", "/admin/product/img.aspx?productId=" + product.Id);
            }
            var product2 = ProductService._.Get(product.Id);
            product2.Info = product.Info;
            product2.Name = product.Name;
            product2.ProductTypeId = product.ProductTypeId;
            product2.Py = product.Name.HzToPySzm() + " " + product.Name.HzToPy();
            ProductService._.Update(product2);
            return new RedirectOfUpdateResult(returnUrl);
        }

        public ActionResult List(
            int? pageIndex,
            string name,
            string productTypeId,
            DateTime? startTime,
            DateTime? endTime)
        {
            const int pageSize = 20;
            int dataCount;
            var list = ProductService._.GetList((pageIndex ?? 1) - 1, pageSize, out dataCount, name, productTypeId, startTime, endTime);
            var pagerList = new PagerList<Product>(list, pageIndex ?? 1, pageSize, dataCount);
            return View(pagerList);
        }

        public ActionResult Del(string id, string returnUrl)
        {
            ProductService._.Delete(id);
            return new RedirectOfDeleteResult(returnUrl);
        }

        public ActionResult Img(string productId, string id)
        {
            var productImgViewModel = new ProductImgViewModel
                                          {
                                              GetProductImgList = ProductImgService._.GetList(productId)
                                          };
            if (!string.IsNullOrEmpty(id))
            {
                productImgViewModel.GetProductImg = ProductImgService._.Get(id);
            }
            return View(productImgViewModel);
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Img(ProductImg productImg)
        {
            if (string.IsNullOrEmpty(productImg.Id))
            {
                productImg.Id = Guid.NewGuid().ToString("N");
                productImg.UrlBig = productImg.UrlBig.MoveFile("/file/product/");
                productImg.UrlMiddle = productImg.UrlMiddle.MoveFile("/file/product/");
                productImg.UrlSmall = productImg.UrlSmall.MoveFile("/file/product/");
                ProductImgService._.Save(productImg);
                return new RedirectOfAddResult("/admin/product/img.aspx?productId=" + productImg.ProductId);
            }
            var productImg2 = ProductImgService._.Get(productImg.Id);
            if (productImg2.UrlBig != productImg.UrlBig)
            {
                productImg2.UrlBig.DeleteFile();
                productImg2.UrlBig = productImg.UrlBig.MoveFile("/file/product/");
                productImg2.UrlMiddle.DeleteFile();
                productImg2.UrlMiddle = productImg.UrlMiddle.MoveFile("/file/product/");
                productImg2.UrlSmall.DeleteFile();
                productImg2.UrlSmall = productImg.UrlSmall.MoveFile("/file/product/");
            }
            ProductImgService._.Update(productImg2);
            return new RedirectOfUpdateResult("/admin/product/img.aspx?productId=" + productImg.ProductId);
        }

        public ActionResult DelImg(string productId, string id)
        {
            ProductImgService._.Delete(id);
            return new RedirectOfDeleteResult("/admin/product/img.aspx?productId=" + productId);
        }
    }
}
